package org.example.lease.schedule;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.lease.model.entities.LeaseAgreement;
import org.example.lease.model.enums.LeaseStatus;
import org.example.lease.service.LeaseAgreementService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 宋孟林
 * @description springboot 定时任务
 * @date: 2025/5/24 18:40
 */
@Component
@Slf4j
public class ScheduledTasks {
    @Resource
    private LeaseAgreementService leaseAgreementService;

    /**
     * 0 0 0 * * *
     * 代表 秒 分 时 一个月的某一天，一年的某个月，一周的第几天
     * 每天的0时0分0秒会执行一次
     */
    @Scheduled(cron = "0 0 0 * * *")
    public void checkStatus() {
        leaseAgreementService.update(Wrappers.lambdaUpdate(LeaseAgreement.class)
                .le(LeaseAgreement::getLeaseEndDate, new Date())
                .eq(LeaseAgreement::getStatus, LeaseStatus.SIGNED)
                .set(LeaseAgreement::getStatus, LeaseStatus.WITHDRAWING));
    }
}
